<!DOCTYPE html>
<html>

<head>
    <title>实时汽车业务数据平台登录</title>
    <meta charset="utf-8">
    <meta content="IE=edge" http-equiv="X-UA-Compatible">
    <link crossorigin="anonymous" href="https://cdn.staticfile.org/twitter-bootstrap/5.3.2/css/bootstrap.min.css"
          integrity="sha384-T3c6CoIi6uLrA9TneNEoa7RxnatzjcDSCmG1MXxSR1GAsXEV/Dwwykc2MPK8M2HN" rel="stylesheet">
    <link href="css/style.css" rel="stylesheet" type="text/css">
    <script src="js/jquery-1.11.0.min.js"></script>
</head>

<body class="bg-dark">
<header id="header">
    <nav class="navbar navbar-expand-lg padding-small px-lg-4">
        <div class="container-fluid">
            <div class="main-logo">
                <h3 style="color: white">欢迎登录实时汽车业务数据平台</h3>
            </div>
            <button aria-controls="offcanvasNavbar" class="navbar-toggler mb-0" data-bs-target="#offcanvasNavbar"
                    data-bs-toggle="offcanvas" type="button">
                <iconify-icon class="hamburger-menu" icon="system-uicons:menu-hamburger"></iconify-icon>
            </button>
            <div class="btn-wrap d-flex my-4 my-lg-0">
                <a class="btn btn-linear btn-medium" href="login.html">登录</a>
                <a class="btn btn-outline-linear btn-medium m-0" href="signin.html">注册</a>
            </div>
        </div>
    </nav>
</header>


<section id="hero">
    <div class="container">
        <h2 class="section-title light text-center my-5 py-5">登录</h2>
    </div>
</section>

<section class="padding-medium pt-0" id="signup">
    <div class="container">
        <div class="col-lg-8 offset-lg-2">
            <form class="form-group flex-wrap" id="form1">
                <div class="form-input col-lg-12 my-4">
                    <input class="form-control bg-blue-trans placeholder-color p-4" id="userName" name="uname" placeholder="账号"
                           type="text">
                    <input aria-describedby="passwordHelpBlock" class="form-control bg-blue-trans placeholder-color p-4" id="passWord" name="upass"
                           placeholder="密码"
                           type="password">
                    <label class="py-3 d-flex flex-wrap justify-content-between">
                        <div>
                            <input class="d-inline" required="" type="checkbox">
                            <span class="label-body fw-medium">保存密码</span>
                        </div>
                        <div class="form-text" id="passwordHelpBlock">
                            <!--                            <a href="signin.html" class="text-primary fw-medium" onclick="aaa()">忘记密码?</a>-->
                        </div>
                    </label>
                    <div class="d-grid my-3">
                        <input class="btn btn-linear btn-medium" onclick="login()" type="button" value="登录">
                    </div>
                    <div class="form-text" id="passwordHelpBlocks">
                        <a class="text-primary fw-medium" href="https://www.sxzy.edu.cn/">遇到问题？联系我们</a>
                    </div>
                </div>
            </form>
        </div>
    </div>
</section>

<script type="text/javascript">

    document.addEventListener('DOMContentLoaded', function () {
        // 获取密码输入框
        const passwordInput = document.getElementById('passWord');

        // 添加键盘事件监听器
        passwordInput.addEventListener('keydown', function (event) {
            // 检查是否按下了回车键
            if (event.key === 'Enter') {
                // 触发登录函数
                login();
            }
        });
    });

    function login() {
        var name = $("#userName").val();
        var pass = $("#passWord").val();

        // 发起 AJAX 请求到后端 API
        $.ajax({
            type: 'POST',
            url: 'http://127.0.0.1:8080/login',  // 后端服务器的地址
            data: {
                uname: name,
                upass: pass
            },
            success: function (data) {
                if (data.re == 2) {
                    const userName = $("#userName").val();
                    sessionStorage.setItem("nowUname", userName); // 存储到会话存储
                    var root = data.uroot
                    var uid = data.uid
                    var uname = data.uname

                    if (root == 0) {
                        document.getElementById('userName').value = null;
                        document.getElementById('passWord').value = null;
                        alert("该账号已冻结，请联系管理员")

                    } else {
                        window.location.href = "main.html?uroot=" + root + "&uid=" + uid + "&uname=" + uname
                    }
                } else if (data.re == 3) {
                    alert("用户名或密码错误")
                } else if (data.re == 4) {
                    alert("用户名或密码错误")
                }
            },
            error: function () {
                alert("用户名不存在");
            }
        });
    }

    function aaa() {
        alert("密码都记不住？？？")
        alert("重新注册去吧")
    }
</script>

</body>

</html>
